package com.yonyou.pmclouds.projectstatus.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.projectstatus.entity.ProjectStatusVO;
import com.yonyou.pmclouds.projectstatus.mapper.ProjectStatusMapper;
import com.yonyou.pmclouds.projectstatus.rmiitf.ProjectStatusQuery;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = ProjectStatusQuery.class)
public class ProjectStatusQueryImpl implements ProjectStatusQuery {
    @Autowired
    private ProjectStatusMapper mapper;

    @Override
    public ProjectStatusVO queryById(String id) throws BusinessException {
        return mapper.getById(id);
    }

    @Override
    public ProjectStatusVO queryByPkSrc(String pkSrc) throws BusinessException {
        String tenantid = RuntimeEnvironment.getTenantId();
        ProjectStatusVO[] projectStatusVO = mapper.queryVOBySrcIds(tenantid,new String[]{pkSrc});
        if(ArrayUtils.isEmpty(projectStatusVO))
            return null;
        return projectStatusVO[0];
    }

    @Override
    public ProjectStatusVO[] queryByPkSrcs(String[] pkSrcs) throws BusinessException {
        String tenantid = RuntimeEnvironment.getTenantId();
        ProjectStatusVO[] projectStatusVOs = mapper.queryVOBySrcIds(tenantid,pkSrcs);
        if(ArrayUtils.isEmpty(projectStatusVOs))
            return new ProjectStatusVO[0];
        return projectStatusVOs;
    }

    @Override
    public Map<String, String> queryNamesByIds(String[] ids) throws BusinessException {
        if (ArrayUtils.isEmpty(ids))
            return new HashedMap();
        ProjectStatusVO[] statusVOS = mapper.queryNamesByIds(ids);
        if(ArrayUtils.isEmpty(statusVOS))
            return new HashMap<>();
        Map<String, String> result = new HashMap<>(statusVOS.length);
        for (ProjectStatusVO vo : statusVOS
             ) {
            result.put(vo.getPkProjectStatus(), vo.getSname());
        }
        return result;
    }

    @Override
    public String queryNameById(String id) throws BusinessException {
        return mapper.queryNameById(id);
    }

}
